function res = resid_dim5case3_pscc(x,shat_2,S_2,la,u,mu0,phi,kap0,eta0,be,del,d,Pzz,cz,T_guess,M)
%function solving for equilibrium (given lambda) in case where all firms actively sell in top productivity 
%state, firms randomize in next productivity state, and no firm actively sells in other states

options = optimoptions('fsolve','TolFun',1e-9,'TolX',1e-9,'StepTolerance',1e-9,'Display','none');

shat_1=x(1);al_2=x(2);dim=5;

%solve for theta corresponding to shat
th_res = @(th) la-mu_pscc(th,mu0,eta0)*eta_el_pscc(th,mu0,eta0)/(1-eta_el_pscc(th,mu0,eta0))*shat_1*kap_s_pscc(shat_1,kap0,phi)/(1+eta_pscc(th,mu0,eta0)*shat_1);
[th_1,fval,exit_flag1] = fsolve(th_res,T_guess(1),options);
if min(exit_flag1)<1,disp('issue: case 3');end

%match surplus given active selling behavior
S_1=kap_s_pscc(shat_1,kap0,phi)/eta_pscc(th_1,mu0,eta0)+la/mu_pscc(th_1,mu0,eta0);

%match surpluses given behavior in active selling state, randomizatiom state, and otherwise
S_vec=M*(u*ones(dim,1)-cz+be*(1-del)*(1-d)*(-la*ones(dim,1)-Pzz(:,1)*kap_n_pscc(shat_1,kap0,phi)-Pzz(:,2)*al_2*kap_n_pscc(shat_2,kap0,phi)));

%equilibrium consistency conditions for surplus
res=[S_vec(1)-S_1;S_vec(2)-S_2];
end